{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>名称</th>\n",
       "      <th>代码</th>\n",
       "      <th>最新价</th>\n",
       "      <th>涨跌幅</th>\n",
       "      <th>换手率</th>\n",
       "      <th>涨速</th>\n",
       "      <th>流通市值</th>\n",
       "      <th>行业</th>\n",
       "      <th>主力资金</th>\n",
       "      <th>论股</th>\n",
       "      <th>板块</th>\n",
       "      <th>板块涨跌幅</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>950</th>\n",
       "      <td>中国汽研</td>\n",
       "      <td>601965</td>\n",
       "      <td>19.11</td>\n",
       "      <td>3.35</td>\n",
       "      <td>0.92</td>\n",
       "      <td>0.16</td>\n",
       "      <td>188.0</td>\n",
       "      <td>[\"\\u534e\\u4e3a\\u6982\\u5ff5\", \"\\u534e\\u4e3a\\u6c...</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td>汽车服务</td>\n",
       "      <td>4.55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>东方时尚</td>\n",
       "      <td>603377</td>\n",
       "      <td>3.60</td>\n",
       "      <td>10.09</td>\n",
       "      <td>9.78</td>\n",
       "      <td>0.00</td>\n",
       "      <td>26.0</td>\n",
       "      <td>[\"\\u8f6c\\u503a\\u6807\\u7684\", \"\\u9884\\u4e8f\\u98...</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td>汽车服务</td>\n",
       "      <td>4.55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>844</th>\n",
       "      <td>申华控股</td>\n",
       "      <td>600653</td>\n",
       "      <td>1.74</td>\n",
       "      <td>3.57</td>\n",
       "      <td>2.14</td>\n",
       "      <td>0.00</td>\n",
       "      <td>34.0</td>\n",
       "      <td>[\"\\u957f\\u6c5f\\u4e09\\u89d2\", \"\\u878d\\u8d44\\u87...</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td>汽车服务</td>\n",
       "      <td>4.55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1182</th>\n",
       "      <td>国机汽车</td>\n",
       "      <td>600335</td>\n",
       "      <td>7.85</td>\n",
       "      <td>3.02</td>\n",
       "      <td>1.90</td>\n",
       "      <td>-0.13</td>\n",
       "      <td>117.0</td>\n",
       "      <td>[\"\\u592e\\u4f01\\u6539\\u9769\", \"\\u4e00\\u5e26\\u4e...</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td>汽车服务</td>\n",
       "      <td>4.55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>172</th>\n",
       "      <td>北巴传媒</td>\n",
       "      <td>600386</td>\n",
       "      <td>4.03</td>\n",
       "      <td>7.47</td>\n",
       "      <td>10.40</td>\n",
       "      <td>-0.25</td>\n",
       "      <td>32.0</td>\n",
       "      <td>[\"\\u6c7d\\u8f66\\u62c6\\u89e3\", \"\\u72ec\\u89d2\\u51...</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td>汽车服务</td>\n",
       "      <td>4.55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>578</th>\n",
       "      <td>荣丰控股</td>\n",
       "      <td>000668</td>\n",
       "      <td>8.91</td>\n",
       "      <td>4.21</td>\n",
       "      <td>3.04</td>\n",
       "      <td>0.11</td>\n",
       "      <td>13.0</td>\n",
       "      <td>[\"\\u5fae\\u76d8\\u80a1\", \"\\u533b\\u7597\\u5668\\u68...</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td>医疗器械</td>\n",
       "      <td>2.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>合富中国</td>\n",
       "      <td>603122</td>\n",
       "      <td>7.32</td>\n",
       "      <td>10.08</td>\n",
       "      <td>11.07</td>\n",
       "      <td>0.00</td>\n",
       "      <td>13.0</td>\n",
       "      <td>[\"\\u673a\\u6784\\u91cd\\u4ed3\", \"\\u4f53\\u5916\\u8b...</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td>医疗器械</td>\n",
       "      <td>2.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>940</th>\n",
       "      <td>黄山胶囊</td>\n",
       "      <td>002817</td>\n",
       "      <td>7.05</td>\n",
       "      <td>3.37</td>\n",
       "      <td>2.30</td>\n",
       "      <td>0.14</td>\n",
       "      <td>20.0</td>\n",
       "      <td>[\"\\u533b\\u7597\\u5668\\u68b0\\u6982\\u5ff5\", \"\\u80...</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td>医疗器械</td>\n",
       "      <td>2.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>济南高新</td>\n",
       "      <td>600807</td>\n",
       "      <td>3.27</td>\n",
       "      <td>10.10</td>\n",
       "      <td>3.11</td>\n",
       "      <td>0.00</td>\n",
       "      <td>26.0</td>\n",
       "      <td>[\"\\u673a\\u6784\\u91cd\\u4ed3\", \"\\u533b\\u7597\\u56...</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td>医疗器械</td>\n",
       "      <td>2.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>962</th>\n",
       "      <td>尚荣医疗</td>\n",
       "      <td>002551</td>\n",
       "      <td>3.10</td>\n",
       "      <td>3.33</td>\n",
       "      <td>4.30</td>\n",
       "      <td>0.00</td>\n",
       "      <td>19.0</td>\n",
       "      <td>[\"\\u9884\\u76c8\\u9884\\u589e\", \"\\u533b\\u7597\\u56...</td>\n",
       "      <td>0</td>\n",
       "      <td></td>\n",
       "      <td>医疗器械</td>\n",
       "      <td>2.95</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        名称      代码    最新价    涨跌幅    换手率    涨速   流通市值  \\\n",
       "950   中国汽研  601965  19.11   3.35   0.92  0.16  188.0   \n",
       "42    东方时尚  603377   3.60  10.09   9.78  0.00   26.0   \n",
       "844   申华控股  600653   1.74   3.57   2.14  0.00   34.0   \n",
       "1182  国机汽车  600335   7.85   3.02   1.90 -0.13  117.0   \n",
       "172   北巴传媒  600386   4.03   7.47  10.40 -0.25   32.0   \n",
       "...    ...     ...    ...    ...    ...   ...    ...   \n",
       "578   荣丰控股  000668   8.91   4.21   3.04  0.11   13.0   \n",
       "43    合富中国  603122   7.32  10.08  11.07  0.00   13.0   \n",
       "940   黄山胶囊  002817   7.05   3.37   2.30  0.14   20.0   \n",
       "40    济南高新  600807   3.27  10.10   3.11  0.00   26.0   \n",
       "962   尚荣医疗  002551   3.10   3.33   4.30  0.00   19.0   \n",
       "\n",
       "                                                     行业  主力资金 论股    板块  板块涨跌幅  \n",
       "950   [\"\\u534e\\u4e3a\\u6982\\u5ff5\", \"\\u534e\\u4e3a\\u6c...     0     汽车服务   4.55  \n",
       "42    [\"\\u8f6c\\u503a\\u6807\\u7684\", \"\\u9884\\u4e8f\\u98...     0     汽车服务   4.55  \n",
       "844   [\"\\u957f\\u6c5f\\u4e09\\u89d2\", \"\\u878d\\u8d44\\u87...     0     汽车服务   4.55  \n",
       "1182  [\"\\u592e\\u4f01\\u6539\\u9769\", \"\\u4e00\\u5e26\\u4e...     0     汽车服务   4.55  \n",
       "172   [\"\\u6c7d\\u8f66\\u62c6\\u89e3\", \"\\u72ec\\u89d2\\u51...     0     汽车服务   4.55  \n",
       "...                                                 ...   ... ..   ...    ...  \n",
       "578   [\"\\u5fae\\u76d8\\u80a1\", \"\\u533b\\u7597\\u5668\\u68...     0     医疗器械   2.95  \n",
       "43    [\"\\u673a\\u6784\\u91cd\\u4ed3\", \"\\u4f53\\u5916\\u8b...     0     医疗器械   2.95  \n",
       "940   [\"\\u533b\\u7597\\u5668\\u68b0\\u6982\\u5ff5\", \"\\u80...     0     医疗器械   2.95  \n",
       "40    [\"\\u673a\\u6784\\u91cd\\u4ed3\", \"\\u533b\\u7597\\u56...     0     医疗器械   2.95  \n",
       "962   [\"\\u9884\\u76c8\\u9884\\u589e\", \"\\u533b\\u7597\\u56...     0     医疗器械   2.95  \n",
       "\n",
       "[100 rows x 12 columns]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import redis\n",
    "import pandas as pd\n",
    "from service import *\n",
    "import json\n",
    "\n",
    "df_global = get_filter_data(col=\"涨跌幅\")\n",
    "bk_df_josn = r_con.get(\"bk_data\")\n",
    "bk_list = json.loads(bk_df_josn)\n",
    "bk_list = bk_list[0:10]\n",
    "def filter_bk(row):\n",
    "    try:\n",
    "        stock_bk_list = json.loads(row[\"行业\"])\n",
    "        for bk in bk_list:\n",
    "            name = bk[\"板块名称\"]\n",
    "            if name in stock_bk_list:\n",
    "                return True \n",
    "    except Exception as err:\n",
    "        pass\n",
    "    return False\n",
    "\n",
    "def add_bk(row):\n",
    "    try:\n",
    "        stock_bk_list = json.loads(row[\"行业\"])\n",
    "        for bk in bk_list:\n",
    "            name = bk[\"板块名称\"]\n",
    "            if name in stock_bk_list:\n",
    "                return name\n",
    "    except Exception as err:\n",
    "        pass\n",
    "    return \"\"\n",
    "\n",
    "def add_bk_zdf(row):\n",
    "    try:\n",
    "        stock_bk_list = json.loads(row[\"行业\"])\n",
    "        for bk in bk_list:\n",
    "            name = bk[\"板块名称\"]\n",
    "            zdf = bk[\"涨跌幅\"]\n",
    "            if name in stock_bk_list:\n",
    "                return zdf\n",
    "    except Exception as err:\n",
    "        pass\n",
    "    return \"\"\n",
    "\n",
    "df_global = df_global[df_global[\"流通市值\"]<450]\n",
    "df_global = df_global.sort_values(\"涨跌幅\", ascending=False)\n",
    "df_global = df_global[df_global.apply(filter_bk,axis=1)]\n",
    "\n",
    "df_global = df_global.head(100)\n",
    "\n",
    "df_global[\"板块\"] = df_global.apply(add_bk,axis=1)\n",
    "df_global[\"板块涨跌幅\"] = df_global.apply(add_bk_zdf,axis=1)\n",
    "\n",
    "df_global = df_global.sort_values(\"板块涨跌幅\", ascending=False)\n",
    "# df_global.drop([\"板块涨跌幅\", \"板块\"], inplace=True,axis=1)\n",
    "df_global"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
